﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Hotel.WPF.HotelWCFSurIIS;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;

namespace Hotel.WPF.ui.CaracteristikHotel
{
    /// <summary>
    /// Interaction logic for P_RefCaracteristiqueHotel.xaml
    /// </summary>
    public partial class P_RefCaracteristiqueHotel : Page
    {
        HotelClient srvService;
        public P_RefCaracteristiqueHotel()
        {
            InitializeComponent();
        }

        private void writeToBusyTool(string message)
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                (SendOrPostCallback)delegate
                {
                    busyTool.BusyContent = message;
                },
                null
                );
        }

        //chargement des ref caractéristiques hotels
        private void loadListeRefCaractHotel()
        {
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    writeToBusyTool("Chargement des caractéristiques d'hotel...");
                    //srvService = new HotelClient();
                    if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                    {
                        writeToBusyTool("Connection au serveur...");
                        srvService.Open();
                    }
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        dg_refCaractHotel.DataContext = srvService.getListRefCaracteristiqueHotels();
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                        //srvService.Close();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Caractéristique Hotel", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void bt_addRefCarctHotel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (tb_codeCaracHotel.Text.Trim().Length==0 || tb_descCaractHotel.Text.Trim().Length==0)
                {
                    MessageBox.Show("Veuillez remplir les champs du formulaire","Ajout",MessageBoxButton.OK,MessageBoxImage.Stop);
                    return;
                }
                Ref_Caracteristique_Hotels rch = new Ref_Caracteristique_Hotels();

                rch.Code_Caracteristique = tb_codeCaracHotel.Text.Trim();
                rch.Description_Caracterisque = tb_descCaractHotel.Text.Trim();
                //srvService = new HotelClient();
                if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                {
                    //writeToBusyTool("Connection au serveur...");
                    srvService.Open();
                }
                int resultatAjout = srvService.AddRefCaracteristiqueHotels(rch);

                if (resultatAjout > 0)
                {
                    MessageBox.Show("Enregistrement réussi",
                                    "Ajout Caractéristique Hotel",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                    loadListeRefCaractHotel();
                }
                else
                {
                    MessageBox.Show("Erreur d'enresgistrement",
                                    "Ajout Caractéristique Hotel",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                //srvService.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ajout Caractéristique Hotel", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            busyTool.Width = SystemParameters.WorkArea.Width;
            busyTool.Height = SystemParameters.WorkArea.Height;

            gridPrincipal.Width = SystemParameters.WorkArea.Width;
            gridPrincipal.Height = SystemParameters.WorkArea.Height - 25;

            #region etablissement connexion avec serveur

            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    srvService = new HotelClient();
                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        loadListeRefCaractHotel();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            #endregion
        }

        private void dg_refCaractHotel_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var element = e.MouseDevice.DirectlyOver;
                if (element != null && element is FrameworkElement)
                {
                    var grid = sender as DataGrid;

                    if (((FrameworkElement)element).Parent is DataGridCell)
                    {
                        if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
                        {
                            var rch = grid.SelectedItem as Ref_Caracteristique_Hotels;

                            tb_codeCaracHotel.Text = rch.Code_Caracteristique;
                            tb_descCaractHotel.Text = rch.Description_Caracterisque;

                            bt_addRefCarctHotel.IsEnabled = false;
                            bt_modifRefCarctHotel.IsEnabled = true;
                        }
                    }
                    else
                    {
                        if (grid != null) grid.SelectedItem = null;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_modifRefCarctHotel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (tb_codeCaracHotel.Text.Trim().Length == 0 || tb_descCaractHotel.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Veuillez remplir les champs du formulaire", "Ajout", MessageBoxButton.OK, MessageBoxImage.Stop);
                    return;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_cancelRefCarac_Click(object sender, RoutedEventArgs e)
        {
            tb_codeCaracHotel.Text = "";
            tb_descCaractHotel.Text = "";
            bt_addRefCarctHotel.IsEnabled = true;
            bt_modifRefCarctHotel.IsEnabled = false;
        }

        private void gererListeCaractHotel(object sender, RoutedEventArgs e)
        {

        }
    }
}
